using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.Workflow.Bases;
using Deepcode.Flux.Core.Services.State;
using Deepcode.Flux.Core.Extensions;
using Deepcode.Flux.Core.Systems.Audit.Quick;

public partial class Private_Config_Workflow_ui_newprocessform : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxWorkflowEdit"; } }
	#endregion
	#region ViewState storage
	/// <summary>
	/// Stores the ID of the handler which this form
	/// is adding a new definition to.
	/// </summary>
	public string HandlerID
	{
		get
		{
			object o = ViewState["HandlerID"];
			if (o == null) return String.Empty;
			return (string)o;
		}
		set
		{
			ViewState["HandlerID"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the definition being edited
	/// </summary>
	public int DefinitionID
	{
		get
		{
			object o = ViewState["DefinitionID"];
			if (o == null) return -1;
			return (int)o;
		}
		set
		{
			ViewState["DefinitionID"] = value;
		}
	}
	#endregion
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			// Store any state information from the calling listing
			this.HandlerID = GetAttribute("HandlerID", "");
			this.DefinitionID = GetAttribute("DefinitionID", -1);

			// Load info 
			Handler h = new Handlers().GetHandlerByID( this.HandlerID );
			if( h == null ) GoBack();
			Definition d = new Definitions().GetByID( this.DefinitionID );
			if( d == null ) GoBack();
			
			// Display infos
			labHandler.Text = h.Name;
			labDefinition.Text = d.DefName;
		}
	}
	#endregion
	#region Navigation
	/// <summary>
	/// Returns to the listing, restoring state
	/// </summary>
	protected void GoBack()
	{
		string url = String.Format(
			"ui_Processes.aspx?HandlerID={0}&DefinitionID={1}", 
			this.HandlerID,
			this.DefinitionID);
		
		Response.Redirect(url, true);
	}
	/// <summary>
	/// Navigate forward to the process editor form
	/// </summary>
	protected void GoForward( int ProcessID )
	{
		string url = String.Format(
			"ui_processform.aspx?HandlerID={0}&DefinitionID={1}&ProcessID={2}",
			this.HandlerID,
			this.DefinitionID,
			ProcessID);

		Response.Redirect( url , true );
	}
	#endregion
	#region Save/Cancel handlers
	/// <summary>
	/// User cancelled the save, go back to previous page
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e)
	{
		GoBack();
	}
	/// <summary>
	/// User clicked the save button, store the data
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (!Page.IsValid) return;
		
		Processes procs = new Processes();
		Process p = new Process();
		
		p.FK_DefinitionID = this.DefinitionID;
		p.ProcessName = this.ProcessName.Text;
		
		if( ProcessType.SelectedValue == "0" )
			p.ProcessType = Deepcode.Flux.Core.Systems.Workflow.Enums.WorkflowProcessType.PROCESS_WAIT;
		else
			p.ProcessType = Deepcode.Flux.Core.Systems.Workflow.Enums.WorkflowProcessType.PROCESS_CLEAR;

		p.Rank = Int32.Parse(this.Rank.Text);
		p.DataCanView = this.DataCanView.Checked;
		p.DataCanEdit = this.DataCanEdit.Checked;

		int ProcessID = procs.Save( p );

		QuickAudit.RaiseInformation(
			new AuditUser(),
			"Flux", "Workflow Manager",
			"created/edited workflow process ({0}) in ({1})",
			p.ProcessName,
			p.ParentDefinition.DefName );

		
		GoForward( ProcessID );
	}
	#endregion
}
